package de.tudarmstadt.ukp.wikisniffer.userstudy.gui.questions;

import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.io.IOException;

import de.tudarmstadt.ukp.wikisniffer.userstudy.Client;
import de.tudarmstadt.ukp.wikisniffer.userstudy.datastore.ResultStore.Task;
import de.tudarmstadt.ukp.wikisniffer.userstudy.gui.AbstractQuestionGui;
import de.tudarmstadt.ukp.wikisniffer.userstudy.gui.questions.util.StopCounterListener;
import de.tudarmstadt.ukp.wikisniffer.userstudy.util.Answer;
import de.tudarmstadt.ukp.wikisniffer.userstudy.util.Question;

@SuppressWarnings("serial")
public class MultipleChoiceQuestion extends AbstractQuestionGui {

	private CheckboxGroup group;
	private Question question;

	public void init(Question question){
		this.question = question;
		textFieldContent = question.getQuestion();
	}
	
	protected Panel getContentPanel() {
		
		Panel content=new Panel();
		content.setLayout(new FlowLayout(FlowLayout.LEADING,60,0));
		
		String[] answers = question.getRandomAnswers();
		
		group = new CheckboxGroup();

		Checkbox option0 = new Checkbox(answers[0],group,false);
		option0.setName(answers[0]);
		option0.addFocusListener(new StopCounterListener());
		content.add(option0);

		Checkbox option1 = new Checkbox(answers[1],group,false);
		option1.setName(answers[1]);
		option1.addFocusListener(new StopCounterListener());
		content.add(option1);

		Checkbox option2 = new Checkbox(answers[2],group,false);
		option2.setName(answers[2]);
		option2.addFocusListener(new StopCounterListener());
		content.add(option2);

		Checkbox option3 = new Checkbox(answers[3],group,false);
		option3.setName(answers[3]);
		option3.addFocusListener(new StopCounterListener());
		content.add(option3);

		Checkbox option4 = new Checkbox("Weiß nicht",group,false);
		option4.setName("Weiß nicht");
		option4.addFocusListener(new StopCounterListener());
		content.add(option4);

				
		return content;
	}

	@Override
	public Answer getAnswer() {
		
		String selectedBox;
		
		if(group.getSelectedCheckbox() != null){
			selectedBox = group.getSelectedCheckbox().getName();
		}
		else{
			selectedBox = "not choosen";
		}
		
		Answer answer = new Answer();
		answer.setAnswer(selectedBox, question);
		
		return answer;
	}

	@Override
	protected void startCounter() {
		try {
			Client.client.resultStore.startTask(Task.QUESTION, textFieldContent);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}	
}
